const tui = {
	toast: function(text, duration, success, isShow) {
		uni.showToast({
			title: text,
			icon: success ? 'success' : 'none',
			duration: duration || 2000
		})
	},
	interfaceUrl: function() {
		//接口地址
		// return "" // 线上
		// return "" // 测试
		return "https://jinrizhizu.ketusoft.com/dynamic-api" //本地
	},
	baseUrl: "https://jinrizhizu.ketusoft.com/dynamic-api",
	request: (method = 'get', url, postData = {}, header = {}, showLoading = false, isShow = true) => {
		const baseUrl = "https://jinrizhizu.ketusoft.com/dynamic-api"
		//接口请求
		const vm = this
		if (showLoading) {
			uni.showLoading({
				mask: true,
				title: '请稍候...'
			})
		}
		postData.forntNowTime = new Date().getTime()
		return new Promise((resolve, reject) => {
			uni.request({
				url: baseUrl + url,
				data: postData,
				header: {
					// 'Content-Type': 'application/x-www-form-urlencoded',
					'openId': uni.getStorageSync("openId") || '',
					... header
				},
				method: method, //'GET','POST'
				dataType: 'json',
				success: (res) => {
					showLoading && uni.hideLoading()
					if (res.data && res.data.msg) {
						res.data.msg = res.data.msg.replace(/<br\s*\/?>/gi,"\n");
					}
					if (res.data.msgCode === -1000) {
						uni.showToast({
							icon: 'none',
							title: '登录过期,请重新登录',
							duration: 3000
						})
						uni.setStorageSync('openId', '')
						uni.reLaunch({
							url: '/pages/me/index'
						})
					}
					if (!res.data.success && isShow) {
						uni.showToast({
							icon: 'none',
							title: res.data.msg
						})
					}
					resolve(res.data)
				},
				fail: (res) => {
					console.log(res,'res')
					if (this.getNetWork()) {
						uni.showToast({
							icon: 'none',
							title: '无可用网络'
						})
					} else {
						uni.showToast({
							icon: 'none',
							title: '请求失败,请检查网络或稍后重试'
						})
					}
					showLoading && uni.hideLoading()
					reject(res)
				}
			})
		})
	},
	uploadImg: function() {
		return new Promise((resolve, reject) => {
			const baseUrl = "https://jinrizhizu.ketusoft.com/dynamic-api/"
			uni.chooseImage({
				success: (chooseImageRes) => {
					// 获取的格式是数组，多选会同时返回，单选只返回一项
					const tempFilePaths = chooseImageRes.tempFilePaths;
					// 若多选，需循环调用uni.uploadFile ，因微信小程序只支持单文件上传
					
					uni.showLoading({
						title: '请稍候...'
					})
					uni.uploadFile({
						url: `${baseUrl}/app/upload/cloud`,
						filePath: tempFilePaths[0],
						name: 'file',
						header: {
							'openId': uni.getStorageSync("openId") || ''
						},
						success: (uploadFileRes) => {
							// 根据接口具体返回格式   赋值具体对应url
							console.log(uploadFileRes, 'uploadFileRes.data')
							const data = uploadFileRes.data ? JSON.parse(uploadFileRes.data) : {}
							if (data.success) {
								resolve(data.data)
							} else {
								reject(data)
							}
						},
						complete: () => {
							uni.hideLoading(); 
						}
					});
				}
			});
			// const uploadTask = uni.uploadFile({
			// 	url: this.interfaceUrl() + url,
			// 	filePath: imgPath,
			// 	name: fileName ? fileName : 'avatar',
			// 	header: {
			// 		// 'content-type': 'multipart/form-data',
			// 		'openId': uni.getStorageSync("openId")
			// 	},
			// 	formData: postData,
			// 	success: function(res) {
			// 		uni.hideLoading()
			// 		res.data = JSON.parse(res.data)
			// 		if (res.data.code !== 0) {
			// 			that.toast(res.data.msg)
			// 		}
			// 		resolve(res.data)
			// 	},
			// 	fail: function(res) {
			// 		reject(res)
			// 		uni.hideLoading();
			// 		that.toast(res.message);
			// 	}
			// })
		})
	},
	getUserInfo() {
		return uni.getStorageSync("userInfo") || {}
	},
	getNetWork() {
		uni.getNetworkType({
			complete: e => {
				let networkType = e.networkType || 'none';
				switch (networkType) {
					case 'none':
						uni.showToast({
							icon: 'none',
							title: '请检查您的网络链接'
						})
						return true
						break;
					case '2g':
					case '3g':
					case '4g':
					case '5g':
						break;
				}
			}
		});
	}
}

export default tui
